package com.xx.doublexchat.common.chat.service.strategy.msg;

import com.xx.doublexchat.common.chat.domain.entity.Message;
import com.xx.doublexchat.common.chat.domain.entity.msg.MsgRecall;
import com.xx.doublexchat.common.chat.domain.enums.MessageTypeEnum;
import com.xx.doublexchat.common.user.cache.UserCache;
import com.xx.doublexchat.common.user.cache.UserInfoCache;
import com.xx.doublexchat.common.user.domain.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 撤回消息策略类
 */
@Component
public class RecallMsgHandler extends AbstractMsgHandler<Object>{

    @Autowired
    private UserInfoCache userInfoCache;

    @Override
    public MessageTypeEnum getMsgTypeEnum() {
        return MessageTypeEnum.RECALL;
    }

    @Override
    protected void saveMsg(Message message, Object body) {
        //抛出不支持的API报错
        throw new UnsupportedOperationException();
    }

    @Override
    public Object showMsg(Message message) {
        MsgRecall recall = message.getExtra().getRecall();
        User userInfo = userInfoCache.get(recall.getRecallUid());
        if (!Objects.equals(recall.getRecallUid(), message.getFromUid())) {
            return "管理员\"" + userInfo.getName() + "\"撤回了一条成员消息";
        }
        return "\"" + userInfo.getName() + "\"撤回了一条消息";
    }

    @Override
    public Object showReplyMsg(Message msg) {
        return "原消息已被撤回";
    }

    @Override
    public String showContactMsg(Message msg) {
        return "撤回了一条消息";
    }
}
